//
//  HWWelfareCenterHeaderView.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/8/24.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWWelfareCenterHeaderView: UIView {
    var topBgView:UIView!
    var itemViews:[UIView] = []
    var titleLabels:[UILabel] = []
    
    var funcView:HWWelfareCenterFuncView!
    
    var itemClick:((_:HWWelfareCenterDataModel)->())?
    var model:HWWelfareCenterModel? {
        didSet {
            
            itemViews.removeAll()
    
            var tempView:UIView!
            for i in 0..<(model?.topdata.count ?? 0) {
                let itemView = UIView()
                topBgView.addSubview(itemView)
                let imageView = UIImageView()
                itemView.addSubview(imageView)
                imageView.sd_setImage(with: URL(string: model?.topdata[i].icon ?? ""), completed: nil)
                
                let titleLabel = HWLabel.createBoldLabel(title: "", color: "#FFFFFF", font: 14*widthScale)
                itemView.addSubview(titleLabel)
                titleLabel.text = model?.topdata[i].name
                
                let button = UIButton(type: .custom)
                button.addTarget(self, action: #selector(itemButtonClick(_:)), for: .touchUpInside)
                button.tag = i
                itemView.addSubview(button)
                
                if tempView != nil {
                    if (model?.topdata.count ?? 0) > 3 {
                        if i % 3 == 0 {
                            itemView.snp.makeConstraints { make in
                                make.left.equalToSuperview()
                                make.top.equalTo(tempView.snp.bottom)
                                make.width.equalTo(screenWidth/3)
                                make.height.equalTo(92*widthScale)
                            }
                        }else {
                            itemView.snp.makeConstraints { make in
                                make.left.equalTo(tempView.snp.right)
                                make.top.equalTo(tempView)
                                make.width.equalTo(screenWidth/3)
                                make.height.equalTo(92*widthScale)
                            }
                        }
                    }else {
                        if i % (model?.topdata.count ?? 0) == 0 {
                            itemView.snp.makeConstraints { make in
                                make.left.equalToSuperview()
                                make.top.equalTo(tempView.snp.bottom)
                                make.width.equalTo(screenWidth/CGFloat(model?.topdata.count ?? 0))
                                make.height.equalTo(92*widthScale)
                            }
                        }else {
                            itemView.snp.makeConstraints { make in
                                make.left.equalTo(tempView.snp.right)
                                make.top.equalTo(tempView)
                                make.width.equalTo(screenWidth/CGFloat(model?.topdata.count ?? 0))
                                make.height.equalTo(92*widthScale)
                            }
                        }
                    }
                    
                }else {
                    itemView.snp.makeConstraints { make in
                        make.left.equalToSuperview()
                        make.top.equalToSuperview()
                        make.height.equalTo(92*widthScale)
                        if (model?.topdata.count ?? 0) > 3 {
                            make.width.equalTo(screenWidth/3)
                        }else {
                            make.width.equalTo(screenWidth/CGFloat(model?.topdata.count ?? 0))
                        }
                    }
                }
                
                imageView.snp.makeConstraints { make in
                    make.top.equalTo(11*widthScale)
                    make.centerX.equalToSuperview()
                    make.width.height.equalTo(42*widthScale)
                }
                
                titleLabel.snp.makeConstraints { make in
                    make.centerX.equalTo(imageView)
                    make.top.equalTo(imageView.snp.bottom).offset(7*widthScale)
                    make.height.equalTo(20*widthScale)
                }
                
                button.snp.makeConstraints { make in
                    make.edges.equalToSuperview()
                }
                
                tempView = itemView
                itemViews.append(itemView)
                titleLabels.append(titleLabel)
            }
            if itemViews.count > 0 {
                itemViews.last?.snp.makeConstraints({ make in
                    make.bottom.equalTo(-9*widthScale)
                })
            }
            funcView.model = model
            funcView.itemClick = {
                self.itemClick?($0)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        topBgView.layoutIfNeeded()
        UIColor.gradientColor(view: topBgView, colors: [UIColor.colorWithHexString(hexString: "#494A56").cgColor,UIColor.colorWithHexString(hexString: "#20202A").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
    }
}

extension HWWelfareCenterHeaderView {
    private func initUI() -> Void {
        
        backgroundColor = .white
        
        topBgView = UIView()
        addSubview(topBgView)
        
        funcView = HWWelfareCenterFuncView()
        addSubview(funcView)
    }
    
    private func initFrame() -> Void {
        topBgView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
        }
        
        funcView.snp.makeConstraints { make in
            make.top.equalTo(topBgView.snp.bottom)
            make.left.right.equalToSuperview()
        }

    }
}

extension HWWelfareCenterHeaderView {
    @objc func itemButtonClick(_ button:UIButton) -> Void {
        self.itemClick?(model!.topdata[button.tag])
    }
}
